﻿function getOtherControlOfSameRow(the_obj,ctl_Name)
{
    //event.cancelBubble=true;
    var result;
    desName = ctl_Name;
    if(the_obj.tagName.toLowerCase() != "table" && the_obj.tagName.toLowerCase() != "tbody"){
        var the_tr;
        if ( the_obj.tagName.toLowerCase() != "tr")
        {
            var the_td = get_Element(the_obj,"td");
            if(the_td==null) return;
            the_tr = the_td.parentElement;
        }
        else
        {
            the_tr = the_obj;
        }
        for (j=0;j<the_tr.cells.length;j++)
        {
             result = get_Child_ElementById(the_tr.cells[j],desName);
             if (result != null) break;
        }

    }
    return result;
}

//Get the parent element of the_ele by the_tag. PS: We can't get a tr from a td.
function get_Element(the_ele,the_tag){
    the_tag = the_tag.toLowerCase();
    if(the_ele.tagName.toLowerCase()==the_tag)return the_ele;
    while(the_ele=the_ele.offsetParent){
        if(the_ele.tagName.toLowerCase()==the_tag)return the_ele;
    }
    return(null);
}

//Get a child element of the_ele by id. 
function get_Child_ElementById(the_ele,the_id){
    the_id = the_id.toLowerCase();
    var result;
    if(the_ele.id != null && the_ele.id.toLowerCase().indexOf(the_id) > -1) 
        return the_ele;
    if (the_ele.children != null && the_ele.children[0] != null)
    {
        for (var i=0;i<the_ele.children.length;i++)
        {
            result = get_Child_ElementById(the_ele.children[i],the_id);
            if (result != null)
                break;
        }
    }
    return result;
}


function FindRowByText(the_table,Text,columnIndex)
{
    for(var i=0;i<the_table.rows.length;i++)
    {
        if (the_table.rows[i].cells[columnIndex].innerText.indexOf(Text) >= 0)
        {
            return the_table.rows[i];
        }
    }
}

function GetGridViewControlByName(currentRowFlag,ctrlName)
{
    var targetId = currentRowFlag.split("$");
    var targetIdHead = "";
    for (var i=0;i<targetId.length - 1; i++)
    {
        targetIdHead += targetId[i] + "_";
    }
    return document.getElementById(targetIdHead+ctrlName);
}

function CheckFileSize(fileLimitSize, controlId, MessageFileSizeOverflowed) {

    var file = getOtherControlOfSameRow(event.srcElement, controlId);
    var image = new Image();
    image.dynsrc = file.value;
    var fileSize = image.fileSize;

    if (fileSize - fileLimitSize > 0) {
        alert(MessageFileSizeOverflowed);
        return false;

    }
    else {
        return true;
    }

}

function InventoryNum_onkeypressed(obj) {

    var str = obj.value;
    // 首个字符可以为负
    if (str == '' && String.fromCharCode(event.keyCode) == '-') {
        return true;
    }
    //首个字符不能为.
    if (str == '' && String.fromCharCode(event.keyCode) == '.') {
        return false;
    }

    var arr = str.split('.')
    //不能存在两个.
    if (arr.length == 2 && String.fromCharCode(event.keyCode) == '.') {
        return false;
    }
    //负号后面不能为.
    if (str == '-' && String.fromCharCode(event.keyCode) == '.') {
        return false;
    }

    var flag = InventoryNumVerify(event.keyCode);
    return flag;

    if (!flag || event.ctrlKey) {
        return false;
    }
    return true;
}
function InventoryNumVerify(iInput) {
    if (((iInput > 47) && (iInput < 58)) || (iInput == 46)) {
        return true;
    }
    return false;
}
